/*
 * @(#) ImageController.java 2018年2月8日 下午3:55:03
 *
 * Copyright 2018 CIMIP, Inc. All rights reserved.
 * H-VISIONS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package cn.hc.share.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/Img")
public class ImageController
{

	@RequestMapping("upload.do")
	@ResponseBody
	public Object uploadImage(@RequestParam(value = "imageName") MultipartFile mf, HttpServletRequest req)
	{
		String realPath = req.getSession().getServletContext().getRealPath("uploadFile/images");
		System.out.println("realPath:" + realPath);
		
		File file = new File(realPath);
		if (!file.exists())
		{
			file.mkdirs();
		}
		// 获取源文件
		String filename = mf.getOriginalFilename();
		String[] names = filename.split("\\.");//
		String tempNum = (int) (Math.random() * 100000) + "";
		String uploadFileName = tempNum + System.currentTimeMillis() + "." + names[names.length - 1];
		File targetFile = new File(realPath, uploadFileName);// 目标文件

		// 开始从源文件拷贝到目标文件

		// 传图片一步到位
		try
		{
			mf.transferTo(targetFile);
		} catch (IllegalStateException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}

		Map<String, String> map = new HashMap<String, String>();
		map.put("data", "http://localhost:8088/share/uploadFile/images/" + uploadFileName);// 这里是项目路径
	
		return map;// 将图片地址返回
	}

}
